<script setup>
import { addManagerListAPI, deleteManagerListAPI, getManagerListAPI, updateManagerListAPI, updateManagerStatusAPI } from '@/api/manager'
import ChooseImage from '@/components/ChooseImage.vue'
import FormDrawer from '@/components/FormDrawer.vue'
import { ref } from 'vue'
import { useInitForm, useInitTable } from '@/composables/useCommon'
import ListHeader from '@/components/ListHeader.vue'
import Search from '@/components/Search.vue'
import SearchItem from '@/components/SearchItem.vue'
const roles = ref([])
const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  total,
  currentPage,
  handleStatusChange,
  handleDelete,
  getData,
} = useInitTable(
  {
    searchForm: {
      keyword: ''
    },
    getList: getManagerListAPI,
    onGetListSuccess: (res) => {
      tableData.value = res.list.map((o) => {
        o.statusLoading = false
        return o
      })
      total.value = res.totalCount
      roles.value = res.roles
    },
    delete: deleteManagerListAPI,
    updateStatus: updateManagerStatusAPI
  }
)

const {
  form,
  formDrawerRef,
  formRef,
  drawerTitle,
  handleEdit,
  handleSubmit,
  handleCreate,

} = useInitForm(
  {
    form: {
      username: '',
      password: '',
      role_id: null,
      status: 1,
      avatar: '',
    },
    getData,
    update: updateManagerListAPI,
    add: addManagerListAPI
  }
)

const handleCurrentChange = (val) => {
  currentPage.value = val
  getData()
}


</script>

<template>
  <el-card>
    <!-- 搜索 -->
    <Search :model="searchForm" @search="getData" @reset="resetSearchForm">
      <SearchItem label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="管理员昵称" clearable></el-input>
      </SearchItem>
    </Search>
    <!-- 新增 刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>
    <!-- 表格区域 -->
    <el-table class="mt-3" v-loading="loading" :data="tableData" border stripe>
      <el-table-column label="管理员" width="200" align="center">
        <template #default="scope">
          <div class="flex items-center">
            <!-- 头像 -->
            <el-avatar :size="40" :src="scope.row.avatar">
              <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
            </el-avatar>
            <div class="flex flex-col justify-between items-start ml-5">
              <h6>{{ scope.row.username }}</h6>
              <small>ID:{{ scope.row.id }}</small>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="所属角色" align="center">
        <template #default="{ row }">
          {{ row.role?.name || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="120">
        <template #default="{ row }">
          <el-switch :disabled="row.super === 1" :loading="row.statusLoading" :model-value="row.status"
            :active-value="1" :inactive-value="0" @change="handleStatusChange($event, row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <small v-if="scope.row.super === 1" class="text-sm text-gray-500">暂无操作</small>
          <div v-else>
            <el-button type="primary" size="small" text @click="handleEdit(scope.row)">修改</el-button>
            <el-popconfirm @confirm="handleDelete(scope.row.id)" confirm-button-text="确认" cancel-button-text="取消"
              title="你确定要删除吗?">
              <template #reference>
                <el-button type="primary" size="small" text>删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="flex justify-center mt-auto-3" background layout="prev, pager, next" :total="total"
      :current-page="currentPage" @current-change="handleCurrentChange" />

    <FormDrawer ref="formDrawerRef" :title="drawerTitle + '管理员'" @submit="handleSubmit">
      <el-form :model="form" label-width="80">
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <ChooseImage v-model="form.avatar"></ChooseImage>
        </el-form-item>
        <el-form-item label="所属角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="选择所属角色">
            <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>
